<?php

error_reporting(E_ALL | E_STRICT);

set_include_path(
	get_include_path() . PATH_SEPARATOR 
	. dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'lib'
);

function pharingAutoload($class)
{
	require_once str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
}

spl_autoload_register('pharingAutoload');

function callService($service, $params)
{
	switch ($service) {
		case 'picasa':
            $picasa = new Pharing_Picasa();
            return $picasa->fetch($params['email']);
		case 'flickr':
            $flickr = new Pharing_Flickr('c73e964d786c0c2f36de5d52545fa0e1');
            return $flickr->fetch($params['email']);
	}
	return array();	
}

$images = (isset($_GET['service'])) ? callService($_GET['service'], $_GET) : array();

?>
<html>
<head>
<style>
#wrapper{ width:600px; margin:auto }
label{ width: 180px; float:left; clear:both }
form div{ float:left; width:100%; margin-bottom:20px }
.services{ float:left; width:300px; margin:0; padding:0 }
.services li{
	border:1px solid;
	cursor:pointer;
	list-style:none;
	height:40px;
	text-align:center;
	width:100px;
	padding:5px;
	float:left;
}
.services img{ vertical-align: 50% }
</style>
<script>
window.onload = function() {
	var servicesLi = document.getElementsByTagName("li");
	for (var i = 0, total = servicesLi.length; i < total; i++) {
		servicesLi[i].onclick = function() {
			document.getElementById("service").value = this.id;
		}
	}	
}
</script>
</head>
<body>
<div id="wrapper">
	<h1><a href=".">Pharing</a></h1>
	<form>   		
		<div>
			<label>Choose the service</label>			
            <input id="service" name="service" value="" readonly="readonly" />
        	<ul class="services">
            	<li id="picasa"><img src="assets/img/picasa.gif" alt="Picasa" title="Picasa" /></li>
            	<li id="flickr"><img src="assets/img/flickr.png" alt="Flickr" title="Flickr" /></li>
            </ul>
    	</div>
    	<div>
    		<label for="email">User email</label>
    		<input id="email" name="email" value="" />
    	</div>
    	<div>
    		<input type="submit" name="submit" id="submit" value="Search images" />
    	</div>
	</form>
<?php if(count($images)): ?>	
	<h2>Photos fetched</h2>
	<div id="images">
<?php foreach($images as $image): ?>
		<img src="<?php echo $image ?>" />
<?php endforeach; ?>	
	</div>
<?php endif; ?>	
</div>
</body>
</html>